<template>
  <div class="login-container">
     <!-- 头部 -->
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="@/assets/images/logo.png"/>
      </template>
    </van-nav-bar>
<!-- 表单 -->
 <div class="login-container">
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号', trigger: 'onChange', pattern: /^1[3456789]\d{9}$/  }]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="[{ required: true, message: '请填写密码', trigger: 'onChange', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
        <van-button
        :loading="isLoading"
         block
         round
         type="info"
         native-type="submit"
         loading-text="加载中..."
         >登录</van-button>

      </div>
    </van-form>
  </div>
  </div>

</template>

<script>
import { mapActions } from 'vuex'
import { Notify } from 'vant'

// Notify('通知内容')
export default {
  name: 'Login',
  data () {
    return {
      isLoading: false,
      //  登录表单数据
      formLogin: {
        // 手号
        mobile: '13443212345',
        //   验证码
        code: '246810' // 默认只能用246810后台规定了
      }
    }
  },
  methods: {
    ...mapActions(['asynLoginAction']),
    //   登录按钮 提交事件 只有全部通过校验才会执行这个函数
    async onSubmit () {
      this.isLoading = true
      //   发送请求  把输入的密码和手机号发给请求
      try {
        await this.asynLoginAction({
          tel: this.formLogin.mobile,
          pwd: this.formLogin.code
        })
        //   console.log(rest)
        Notify({ type: 'success', message: '登陆成功' })
      } catch (err) {
        console.log(err)
        Notify({ type: 'warning', message: '手机号和密码错误' })
      }

      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
// .van-nav-bar{
//    background: #007bff;
// }
// /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
// /deep/ .van-nav-bar__title{
// color: white;
// }
</style>
